<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="相机名" prop="sourceName">
        <el-input v-model="queryParams.sourceName" placeholder="请输入相机名" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="日期" prop="time">
        <el-date-picker clearable v-model="queryParams.time" type="date" value-format="yyyy-MM-dd" placeholder="请选择日期"> </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['eab:source:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['eab:source:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['eab:source:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['eab:source:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-RefreshRight" size="mini" @click="restartAlgorithm" v-hasPermi="['eab:source:export']">重新启动</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sourceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align="center" />
      <el-table-column label="相机名" align="center"  prop="sourceName"  width="60"  />
      <el-table-column label="流地址" align="center" prop="streamUrl"  width="180" />
      <el-table-column label="编码格式" align="center" prop="type"  width="75">
        <template slot-scope="scope">{{getLabel(optiontype,scope.row.type,'value','label') }}</template>
      </el-table-column>
      <el-table-column label="检测间隔" align="center" prop="interal"  width="50"  />
      
      <el-table-column label="检测功能" align="center" prop="infer0Type"  width="48">
        <template slot-scope="scope">{{getLabel(optioninfer0Type,scope.row.infer0Type,'value','label') }}</template>
      </el-table-column>
      <el-table-column label="检测置信度" align="center" prop="infer0Scorethreshold"  width="60" />
      <el-table-column label="检测框阈值" align="center" prop="infer0Nmsthreshold"   width="70" />
      <!-- <el-table-column label="识别功能" align="center" prop="infer1Type"  width="50"  /> -->
      <el-table-column label="识别功能" align="center" prop="infer1Type"  width="48">
        <template slot-scope="scope">{{getLabel(optioninfer1Type,scope.row.infer1Type,'value','label') }}</template>
      </el-table-column>
      <el-table-column label="识别置信度" align="center"  prop="infer1Scorethreshold"  width="60" />
      <el-table-column label="识别框阈值" align="center" prop="infer1Nmsthreshold"  width="70"  />
      <!-- <el-table-column label="推流方式" align="center"   prop="sinktype" width="50" /> -->
      <el-table-column label="推流方式" align="center" prop="sinktype"  width="48">
        <template slot-scope="scope">{{getLabel(optionsinktype,scope.row.sinktype,'value','label') }}</template>
      </el-table-column>
      <!-- <el-table-column label="推流编码格式" align="center" prop="sinkCode"   width="60"  /> -->
      <el-table-column label="推流编码格式" align="center" prop="sinkCode"  width="75">
        <template slot-scope="scope">{{getLabel(optiontype,scope.row.sinkCode,'value','label') }}</template>
      </el-table-column>
      <el-table-column label="经度" align="center" prop="lon"  width="50" />
      <el-table-column label="纬度" align="center" prop="lat"  width="50" />
      <el-table-column label="高程" align="center"  prop="altitude"  width="50" />
      <!-- <el-table-column label="配置数据" align="center"  prop="configValue"  width="50" /> -->
      <el-table-column label="日期" align="center"  prop="time" width="120"  >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="启用标志" align="center" prop="enable"  width="50" >
        <template slot-scope="scope">
          <el-switch active-color="#13ce66" inactive-colr="#ff4949" v-model="scope.row.enable"
          :active-value="1" :inactive-value="0" @change="changeSwitch($event,scope.row,scope.$index)">
          </el-switch>
        </template>
      </el-table-column>
       <el-table-column label="算法状态" align="center" prop="status"  width="50" >
        <template slot-scope="scope">
          <el-switch active-color="#13ce66" inactive-colr="#ff4949" v-model="scope.row.status"
          :active-value="1" :inactive-value="0"   >
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['eab:source:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['eab:source:remove']"
          >删除</el-button>
    
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="450px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px" text-align="justify" >

        <el-form-item label="相机名" prop="sourceName">
          <el-input v-model="form.sourceName" placeholder="请输入相机名称" />
        </el-form-item>
        <el-form-item label="相机启用标志" prop="enable"  >
            <el-select v-model="form.enable" placeholder="请选择相机启用标识" style="width:300px">
              <el-option
                v-for="item in optionenable"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="相机流地址" prop="streamUrl">
          <el-input v-model="form.streamUrl" placeholder="请输入流地址" />
        </el-form-item>

        <el-form-item label="编码格式" prop="type"  >
            <el-select v-model="form.type" placeholder="请选择编码格式" style="width:300px">
              <el-option
                v-for="item in optiontype"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="检测间隔" prop="interal">
          <el-input v-model="form.interal" placeholder="请输入检测间隔 2-8" />
        </el-form-item>
        <el-form-item label="检测功能" prop="infer0Type"  >
            <el-select v-model="form.infer0Type" placeholder="请选择检测功能" style="width:300px">
              <el-option
                v-for="item in optioninfer0Type"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="检测置信度" prop="infer0Scorethreshold">
          <el-input v-model="form.infer0Scorethreshold" placeholder="请输入置信度阈值0-1" />
        </el-form-item>
        <el-form-item label="检测框重复阈值" prop="infer0Nmsthreshold">
          <el-input v-model="form.infer0Nmsthreshold" placeholder="请输入检测框重复阈值0-1" />
        </el-form-item>
        <el-form-item label="识别功能" prop="infer1Type"  >
            <el-select v-model="form.infer1Type" placeholder="请选择识别功能" style="width:300px">
              <el-option
                v-for="item in optioninfer1Type"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="识别置信度" prop="infer1Scorethreshold">
          <el-input v-model="form.infer1Scorethreshold" placeholder="请输入识别置信度0-1" />
        </el-form-item>
        <el-form-item label="识别框重复阈值" prop="infer1Nmsthreshold">
          <el-input v-model="form.infer1Nmsthreshold" placeholder="请输入识别框重复阈值0-1" />
        </el-form-item>
        <el-form-item label="推流方式" prop="sinktype"  >
            <el-select v-model="form.sinktype" placeholder="请选择推流方式" style="width:300px">
              <el-option
                v-for="item in optionsinktype"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="推流编码格式" prop="sinkCode"  >
            <el-select v-model="form.sinkCode" placeholder="请选择推流方式" style="width:300px">
              <el-option
                v-for="item in optiontype"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="经度" prop="lon">
          <el-input v-model="form.lon" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="lat">
          <el-input v-model="form.lat" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="高程" prop="altitude">
          <el-input v-model="form.altitude" placeholder="请输入高程" />
        </el-form-item>
        <el-form-item label="配置数据" prop="configValue">
          <el-input v-model="form.configValue" placeholder="请输入配置数据" />
        </el-form-item>
        <el-form-item label="日期" prop="time" >
          <el-date-picker clearable v-model="form.time"
            type="date" value-format="yyyy-MM-dd" placeholder="请选择日期" style="width:300px">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
        <!-- 预览视频流对话框 -->
    <el-dialog title="预览检测" :visible.sync="openPreview" width="1000px" hight="800px" append-to-body>
            <!-- 播放器 -->
        <video
          ref="videoElement"
          :src="videoUrl"
          id="videoElement"
          controls
          muted
          style="width: 100%; height: 100%; object-fit: fill"
        ></video>
   
    </el-dialog>
  </div>
</template>

<script>
import { listSource, getSource, delSource, addSource, updateSource,restartAlgorithm} from "@/api/disposition/device";
import hlsjs from "hls.js";
export default {
  name: "Source",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备表格数据
      sourceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //预览弹出层
      openPreview:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        terminalId: null,
        sourceId: null,
        sourceName: null,
        enable: null,
        streamUrl: null,
        type: null,
        interal: null,
        infer0Type: null,
        infer0Scorethreshold: null,
        infer0Nmsthreshold: null,
        infer1Type: null,
        infer1Scorethreshold: null,
        infer1Nmsthreshold: null,
        sinktype: null,
        sinkCode: null,
        lon: null,
        lat: null,
        altitude: null,
        configValue: null,
        time: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //启用标识
      optionenable: [{value: '0',label: '禁用'}, {value: '1',label: '启用'}],
      //编码格式
      optiontype:   [{value: '264',label: 'H264'}, {value: '265',label: 'H265'}],
      //检测功能
      optioninfer0Type: [{value: '0',label: '空置'}, {value: '1',label: '人脸检测'},{value: '2',label: '车牌检测'}],
      //识别功能
      optioninfer1Type: [{value: '0',label: '空置'}, {value: '3',label: '人脸识别'},{value: '4',label: '车牌识别'}],
      //推流方式
      optionsinktype:   [{value: '0',label: '不推流'}, {value: '1',label: 'rtmp'},{value: '2',label: 'rtsp'}],
      //视频流地址
      // videoUrl: "http://172.18.4.228/live/sink0/hls.fmp4.m3u8",//这里书写路径，例
      videoUrl: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备列表 */
    getList() {
      this.loading = true;
      listSource(this.queryParams).then(response => {
        this.sourceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        terminalId: null,
        sourceId: null,
        sourceName: null,
        enable: null,
        streamUrl: null,
        type: null,
        interal: null,
        delFlag: null,
        infer0Type: null,
        infer0Scorethreshold: null,
        infer0Nmsthreshold: null,
        infer1Type: null,
        infer1Scorethreshold: null,
        infer1Nmsthreshold: null,
        sinktype: null,
        sinkCode: null,
        lon: null,
        lat: null,
        altitude: null,
        configValue: null,
        time: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log("*****************",row)
      this.reset();
      const id = row.id || this.ids
      getSource(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSource(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSource(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除设备编号为"' + ids + '"的数据项？').then(function() {
        return delSource(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('eab/source/export', {
        ...this.queryParams
      }, `source_${new Date().getTime()}.xlsx`)
    },

    changeSwitch(data,b,index){
      this.form = b
      updateSource(this.form).then(response => { 
              this.getList();
            });
    },
    
    /**
       * 根据传入的值,返回对应的中文name，常用的地方是表格那里
       * list: 传入的源数组
       * id: 传入的值
       * value: 源数组中为了匹配id值的字段名称
       * label: 源数组中需要返回显示中文的字段名称
       * 示例：arr:[{dictValue: 0,dictLabel:'前端工程师'},{dictValue: 1,dictLabel:'Java工程师'}]
       * 调用getLabel(arr, 1, "dictValue", "dictLabel")返回了 Java工程师
       * */
    getLabel(list, id, value, label) {
      	if (id != '' && Array.isArray(list) && list.length != 0){
          return !list.find(item => item[value] == id) ? id : list.find(item => item[value] == id)[label]
        } else {
          return id
        }
      },
    //重启算法
    restartAlgorithm(){
      restartAlgorithm().then(response => {
        console.log(response)
      })
    },
  },
  mounted() {
    
  },
};
</script>
